#include "MeleeState.h"

#include "..\Game Objects\GameObject.h"
#include "..\\Game Objects\\Enemy.h"
#include "..\\Game Objects\\Camera.h"

#include <ctime>

MeleeState::MeleeState()
{
	bJustPlayedSmash = false;
}


MeleeState::~MeleeState()
{
}

void MeleeState::Update(Enemy* enemy, float dt)
{
	enemy->SetAttackTimer(enemy->GetAttackTimer() + dt);
	Camera* camera = Camera::GetInstance();


	// Check if Boss
	if (enemy->GetID() == GameObject::ObjectID::QUDZU)
		PlayQudzuAttacks(enemy, dt);
	else if (enemy->GetID() == GameObject::ObjectID::PAPABEAR)
		PlayPapaBearAttacks(enemy, dt);
	else if (enemy->GetID() == GameObject::ObjectID::LOCKJAW)
		PlayLockJawAttacks(enemy, dt);

// Else an enemy
	else if (enemy->IsFacingLeft()) // Enemy moving left
	{
		// Create Attack Rect
		SGD::Rectangle rect;

		// Stop Enemy from moving
		enemy->SetVelocity(SGD::Vector{ 0.0f, enemy->GetVelocity().y });
		enemy->SetAcceleration(SGD::Vector{ 0.0f, enemy->GetAcceleration().y });

		// Set Attacking Left animation
		if (enemy->GetCurrentAnimationName() != "EnemyAttackingLeft")
			enemy->SetAnimation("EnemyAttackingLeft");

		if (enemy->GetAttackTimer() < enemy->GetAttackResetTime() * 0.05f)
		{
			// Set Attack Rect Values
			rect.left = enemy->GetPosition().x - 40 - camera->GetPosition().x;
			rect.top = enemy->GetPosition().y + 70 - camera->GetPosition().y - enemy->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 10;
			rect.bottom = rect.top + 20;
			enemy->SetAttackRect(rect);
		}
		else if (enemy->GetAttackTimer() < enemy->GetAttackResetTime() * 0.1f)
		{
			// Set Attack Rect Values
			rect.left = enemy->GetPosition().x - 80 - camera->GetPosition().x;
			rect.top = enemy->GetPosition().y + 20 - camera->GetPosition().y - enemy->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 60;
			enemy->SetAttackRect(rect);
		}
		else if (enemy->GetAttackTimer() < enemy->GetAttackResetTime() * 0.25f)
		{
			// Set Attack Rect Values
			rect.left = enemy->GetPosition().x - 30 - camera->GetPosition().x;
			rect.top = enemy->GetPosition().y + 10 - camera->GetPosition().y - enemy->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 10;
			rect.bottom = rect.top + 20;
			enemy->SetAttackRect(rect);
		}
		else
		{
			// Set Attack Rect Values
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;
			enemy->SetAttackRect(rect);

			// Set Standing animation
			enemy->SetAnimation("EnemyStandingLeft");
		}
	}
	else // Enemy facing right
	{
		// Stop Enemy from moving
		enemy->SetVelocity(SGD::Vector{ 0.0f, enemy->GetVelocity().y });
		enemy->SetAcceleration(SGD::Vector{ 0.0f, enemy->GetAcceleration().y });

		// Set Attacking right animation
		if (enemy->GetCurrentAnimationName() != "EnemyAttackingRight")
			enemy->SetAnimation("EnemyAttackingRight");

		SGD::Rectangle rect;
		if (enemy->GetAttackTimer() < enemy->GetAttackResetTime() * 0.05f)
		{
			// Set Attack Rect Values
			rect.left = enemy->GetPosition().x + enemy->GetSize().width / 2 - camera->GetPosition().x;
			rect.top = enemy->GetPosition().y + 70 - camera->GetPosition().y - enemy->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 10;
			rect.bottom = rect.top + 20;
			enemy->SetAttackRect(rect);
		}
		else if (enemy->GetAttackTimer() < enemy->GetAttackResetTime() * 0.1f)
		{
			// Set Attack Rect Values
			rect.left = enemy->GetPosition().x + enemy->GetSize().width / 2 - camera->GetPosition().x;
			rect.top = enemy->GetPosition().y + 20 - camera->GetPosition().y - enemy->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 60;
			enemy->SetAttackRect(rect);
		}
		else if (enemy->GetAttackTimer() < enemy->GetAttackResetTime() * 0.25f)
		{
			// Set Attack Rect Values
			rect.left = enemy->GetPosition().x + enemy->GetSize().width / 2 - camera->GetPosition().x;
			rect.top = enemy->GetPosition().y + 10 - camera->GetPosition().y - enemy->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 10;
			rect.bottom = rect.top + 20;
			enemy->SetAttackRect(rect);
		}
		else
		{
			// Set Attack Rect Values
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;
			enemy->SetAttackRect(rect);

			// Set Standing animation
			enemy->SetAnimation("EnemyStandingRight");
		}
	}

	if (enemy->GetAttackTimer() >= enemy->GetAttackResetTime())
	{
		enemy->SetAttackTimer(0.0f);
	}
}

void MeleeState::PlayQudzuAttacks(Enemy* qudzu, float dt)
{
	// Add Attack Rects here
	Camera* camera = Camera::GetInstance();

	SGD::Rectangle rect = {};

	if (qudzu->IsMovingLeft())
	{
		if (qudzu->GetCurrentAnimationName() != "QudzuAttackLeft")
		{
			qudzu->SetAnimation("QudzuAttackLeft");
		}

		if (qudzu->GetAttackTimer() < qudzu->GetAttackResetTime() * 0.4f)
		{
			rect.left = qudzu->GetPosition().x - 90 - camera->GetPosition().x;
			rect.top = qudzu->GetPosition().y + 15 - camera->GetPosition().y - qudzu->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 60;
			rect.bottom = rect.top + 75;

			qudzu->SetAttackRect(rect);
		}
		else
		{
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;

			qudzu->SetAttackRect(rect);

			bJustPlayedSmash = false;

			qudzu->SetAnimation("QudzuIdleLeft");
		}
	}
	else if (!qudzu->IsMovingLeft())
	{
		if (qudzu->GetCurrentAnimationName() != "QudzuAttackRight")
		{
			qudzu->SetAnimation("QudzuAttackRight");
		}

		if (qudzu->GetAttackTimer() < qudzu->GetAttackResetTime() * 0.4f)
		{
			rect.left = qudzu->GetPosition().x + 30 - camera->GetPosition().x;
			rect.top = qudzu->GetPosition().y + 15 - camera->GetPosition().y - qudzu->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 60;
			rect.bottom = rect.top + 75;

			qudzu->SetAttackRect(rect);
		}
		else
		{
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;

			qudzu->SetAttackRect(rect);

			bJustPlayedSmash = false;

			qudzu->SetAnimation("QudzuIdleRight");
		}
	}
}

void MeleeState::PlayPapaBearAttacks(Enemy* papabear, float dt)
{
	// Add Attack Rects here
	Camera* camera = Camera::GetInstance();

	SGD::Rectangle rect = {};

	if (papabear->IsMovingLeft())
	{
		// Set Attacking Left
		if (papabear->GetCurrentAnimationName() != "PapaBearAttackLeft")
			papabear->SetAnimation("PapaBearAttackLeft");
		if (papabear->GetAttackTimer() < papabear->GetAttackResetTime() * 0.05f)
		{
			// Set first rectangle position of attack
			rect.left = papabear->GetPosition().x - 60 - camera->GetPosition().x;
			rect.top = papabear->GetPosition().y + 10 - camera->GetPosition().y - papabear->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 50;
			papabear->SetAttackRect(rect);
		}
		else if (papabear->GetAttackTimer() < papabear->GetAttackResetTime() * 0.1f)
		{
			// Set Second rectangle position of attack
			rect.left = papabear->GetPosition().x - 60 - camera->GetPosition().x;
			rect.top = papabear->GetPosition().y + 10 - camera->GetPosition().y - papabear->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 50;
			papabear->SetAttackRect(rect);
		}
		else if (papabear->GetAttackTimer() < papabear->GetAttackResetTime() * 0.25f)
		{
			// Set Third rectangle position of attack
			rect.left = papabear->GetPosition().x - 60 - camera->GetPosition().x;
			rect.top = papabear->GetPosition().y + 10 - camera->GetPosition().y - papabear->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 50;
			papabear->SetAttackRect(rect);
		}
		else
		{
			// Set to none
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;
			papabear->SetAttackRect(rect);

			// Set Just played to false
			bJustPlayedSmash = false;

			// Set To standing left
			papabear->SetAnimation("PapaBearIdleLeft");
		}
	}
	else if (!papabear->IsMovingLeft())
	{
		// Set Attacking Right
		if (papabear->GetCurrentAnimationName() != "PapaBearAttackRight")
			papabear->SetAnimation("PapaBearAttackRight");

		if (papabear->GetAttackTimer() < papabear->GetAttackResetTime() * 0.05f)
		{
			// Set First rectangle position of attack
			rect.left = papabear->GetPosition().x + 20 - camera->GetPosition().x;
			rect.top = papabear->GetPosition().y + 10 - camera->GetPosition().y - papabear->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 50;
			papabear->SetAttackRect(rect);
		}
		else if (papabear->GetAttackTimer() < papabear->GetAttackResetTime() * 0.1f)
		{
			// Set Second rectangle position of attack
			rect.left = papabear->GetPosition().x + 20 - camera->GetPosition().x;
			rect.top = papabear->GetPosition().y + 10 - camera->GetPosition().y - papabear->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 50;
			papabear->SetAttackRect(rect);
		}
		else if (papabear->GetAttackTimer() < papabear->GetAttackResetTime() * 0.25f)
		{
			// Set Third rectangle position of attack
			rect.left = papabear->GetPosition().x + 20 - camera->GetPosition().x;
			rect.top = papabear->GetPosition().y + 10 - camera->GetPosition().y - papabear->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 50;
			papabear->SetAttackRect(rect);
		}
		else
		{
			// Set to none
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;
			papabear->SetAttackRect(rect);

			// Set Just played to false
			bJustPlayedSmash = false;

			// Set to standing right
			papabear->SetAnimation("PapaBearIdleRight");
		}

	}
}

void MeleeState::PlayLilPrickAttacks(Enemy* lilprick, float dt)
{
	// SINCE LIL' PRICK IS MIA
	// PAPA BEAR IS STANDING IN HIS PLACE
	PlayPapaBearAttacks(lilprick, dt);

}

void MeleeState::PlayLockJawAttacks(Enemy* lockjaw, float dt)
{
	srand((unsigned int)time(nullptr));
	Camera* camera = Camera::GetInstance();

	SGD::Rectangle rect = {};
	// Ground Smash?
	int i = rand() % 5;
	if (i == 0 && !bJustPlayedSmash)
	{
		LockJawSmash(lockjaw, dt);
		return;
	}
	if (lockjaw->IsMovingLeft())
	{
		// Reset Attack Rect if changed direction
		if (!lockjaw->PrevDirection())
		{
			lockjaw->SetAttackRect(SGD::Rectangle{ -1, -1, -1, -1 });
			lockjaw->IsAttacking(false);
		}

		// Set Movement/Facing values
		lockjaw->PrevDirection(true);

		// Set Attacking Left
		if (lockjaw->GetCurrentAnimationName() != "LockJawAttackingLeft")
			lockjaw->SetAnimation("LockJawAttackingLeft");
		if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.05f)
		{
			// Set first rectangle position of attack
			rect.left = lockjaw->GetPosition().x - 40 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 20 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 20;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.1f)
		{
			// Set Second rectangle position of attack
			rect.left = lockjaw->GetPosition().x - 120 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 20 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.25f)
		{
			// Set Third rectangle position of attack
			rect.left = lockjaw->GetPosition().x - 170 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 20 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 60;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else
		{
			// Set to none
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;
			lockjaw->SetAttackRect(rect);

			// Set Just played to false
			bJustPlayedSmash = false;

			// Set To standing left
			lockjaw->SetAnimation("LockJawStandingLeft");
		}
	}
	else if (!lockjaw->IsMovingLeft())
	{
		// Reset Attack Rect if changed direction
		if (lockjaw->PrevDirection())
		{
			lockjaw->SetAttackRect(SGD::Rectangle{ -1, -1, -1, -1 });
			lockjaw->IsAttacking(false);
		}

		// Set Movement/Facing values
		lockjaw->PrevDirection(false);

		// Set Attacking Right
		if (lockjaw->GetCurrentAnimationName() != "LockJawAttackingRight")
			lockjaw->SetAnimation("LockJawAttackingRight");

		if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.05f)
		{
			// Set First rectangle position of attack
			rect.left = lockjaw->GetPosition().x + 20 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 20 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 20;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.1f)
		{
			// Set Second rectangle position of attack
			rect.left = lockjaw->GetPosition().x + 60 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 20 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 40;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.25f)
		{
			// Set Third rectangle position of attack
			rect.left = lockjaw->GetPosition().x + 110 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 20 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 60;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else
		{
			// Set to none
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;
			lockjaw->SetAttackRect(rect);

			// Set Just played to false
			bJustPlayedSmash = false;

			// Set to standing right
			lockjaw->SetAnimation("LockJawStandingRight");
		}

	}
}

void MeleeState::LockJawSmash(Enemy* lockjaw, float dt)
{
	Camera* camera = Camera::GetInstance();

	SGD::Rectangle rect = {};

	// Smash
	if (lockjaw->IsMovingLeft())
	{
		// Set Attacking left animation
		if (lockjaw->GetCurrentAnimationName() != "LockJawGroundSmashLeft")
			lockjaw->SetAnimation("LockJawGroundSmashLeft");
		if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.05f)
		{
			// Set first rectangle position of attack
			rect.left = lockjaw->GetPosition().x - 40 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 20 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 20;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.1f)
		{
			// Set Second rectangle position of attack
			rect.left = lockjaw->GetPosition().x - 60 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 40 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 20;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.25f)
		{
			// Set Third rectangle position of attack
			rect.left = lockjaw->GetPosition().x - 300 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 150 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 240;
			rect.bottom = rect.top + 10;
			lockjaw->SetAttackRect(rect);
		}
		else
		{
			// Set to none
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;
			lockjaw->SetAttackRect(rect);

			// Set Just Played to true
			bJustPlayedSmash = true;

			// Set To standing left
			lockjaw->SetAnimation("LockJawStandingLeft");
		}
	}
	else if (!lockjaw->IsMovingLeft())
	{
		// Set Attacking right animation
		if (lockjaw->GetCurrentAnimationName() != "LockJawGroundSmashRight")
			lockjaw->SetAnimation("LockJawGroundSmashRight");
		if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.05f)
		{
			// Set First rectangle position of attack
			rect.left = lockjaw->GetPosition().x + 20 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 20 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 20;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.1f)
		{
			// Set Second rectangle position of attack
			rect.left = lockjaw->GetPosition().x + 40 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 40 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 20;
			rect.bottom = rect.top + 50;
			lockjaw->SetAttackRect(rect);
		}
		else if (lockjaw->GetAttackTimer() < lockjaw->GetAttackResetTime() * 0.25f)
		{
			// Set Third rectangle position of attack
			rect.left = lockjaw->GetPosition().x + 60 - camera->GetPosition().x;
			rect.top = lockjaw->GetPosition().y + 150 - camera->GetPosition().y - lockjaw->GetAnimation()->GetDrawRect().ComputeHeight();
			rect.right = rect.left + 240;
			rect.bottom = rect.top + 10;
			lockjaw->SetAttackRect(rect);
		}
		else
		{
			// Set to none
			rect.left = -1;
			rect.top = -1;
			rect.right = -1;
			rect.bottom = -1;
			lockjaw->SetAttackRect(rect);

			// Set Just Played to true
			bJustPlayedSmash = true;

			// Set to standing right
			lockjaw->SetAnimation("LockJawStandingRight");
		}
	}
}